package com.atwork.wuhua.mvp.presenter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.base.BasePresenter;
import com.atwork.wuhua.base.BaseReq;
import com.atwork.wuhua.base.MyApplication;
import com.atwork.wuhua.base.SystemBean;
import com.atwork.wuhua.bean.BannerBean;
import com.atwork.wuhua.bean.FileLoadBean;
import com.atwork.wuhua.bean.RedirectBean;
import com.atwork.wuhua.mvp.Callback;
import com.atwork.wuhua.mvp.DataModel;
import com.atwork.wuhua.mvp.Token;
import com.atwork.wuhua.mvp.view.ISplashActivity;
import com.atwork.wuhua.ui.activity.SplashActivity;
import com.atwork.wuhua.ui.activity.WebViewActivity;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.SPUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SplashPersenter extends BasePresenter<ISplashActivity> {
    private String is_wap = "";
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    intentType();
                    break;
                case 1:
                    Gson gson = new Gson();
                    final RedirectBean entity = gson.fromJson((String) msg.obj,
                            RedirectBean.class);

                    if (entity.getErrno().equals("000")) {
                        if (entity.getResult().getIs_wap() == 1) {
                            SPUtils.setString(getView().getContext(), SPUtils.IS_WAP, entity.getResult().getIs_wap() + "");
                        }

                        is_wap = SPUtils.getString(getView().getContext(), SPUtils.IS_WAP);
                        // 跳转web
                        if (is_wap.equals("1")) {
                            Intent intent = new Intent(getView().getContext(), WebViewActivity.class);
                            intent.putExtra(ConstantsMyself.INTENTURL, entity.getResult().getWap());
                            getView().getContext().startActivity(intent);
                            ((Activity) getView().getContext()).finish();
                        } else {
                            //主页
                            intentType();
                        }
                    } else {
                        //主页
                        intentType();
                    }


                    break;
            }

            super.

                    handleMessage(msg);

        }
    };

    public void getRedirect() {
        String url = "http://103.85.254.88/v3/app/redirect?app_id=" + getView().getContext().getPackageName();
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = new Message();
                message.what = 0;
                handler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Message message = new Message();
                message.what = 1;
                message.obj = string;
                handler.sendMessage(message);
            }

        });
    }

    public void intentType() {
        BaseReq req = new BaseReq();
        req.setKey("package", getView().getContext().getPackageName());
        req.setKey("type", "2");
        DataModel.request(Token.API_SYSTEM)
                .params(req)
                .execute(new Callback<SystemBean>() {

                    public void onSuccess(String msg, SystemBean data) {
                        MyApplication.systemBean = data;
                        getView().intentType(data);

                    }

                    @Override
                    public void onFailure(String msg) {
                        hideLoading();
                    }

                    @Override
                    public void onError() {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
